<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">菜单管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>菜单管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input id="auth-edit-search" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="auth-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="auth-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="auth-table" lay-filter="auth-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="add">添加下级菜单</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'admin', 'treetable', 'jquery'], function () {
        const $ = layui.jquery;
        const table = layui.table;
        const layer = layui.layer;
        const admin = layui.admin;
        const treeTable = layui.treetable;

        /** 渲染表格:插件开源地址：https://gitee.com/whvse/treetable-lay **/
        const renderTable = function () {
            treeTable.render({
                treeColIndex: 1, // 树形图标显示在第几列
                treeSpid: -1, // 最上级的父级id
                treeIdName: 'id', // id字段的名称
                treePidName: 'parentId', // pid字段的名称
                treeDefaultClose: true, // 是否默认折叠
                treeLinkage: false, // 父级展开时是否自动展开所有子级
                elem: '#auth-table',
                url: basePath + 'system/menu/list',
                page: false,
                cols: [
                    [
                        {type: 'numbers'},
                        {field: 'menuName', minWidth: 200, title: '菜单名称'},
                        {field: 'authority', title: '权限标识'},
                        {field: 'menuUrl', title: '菜单url'},
                        {field: 'sort', width: 80, align: 'center', title: '排序号'},
                        {
                            field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                                if (d.isMenu == 1) {
                                    return '<span class="layui-badge layui-bg-orange">按钮</span>';
                                }
                                if (d.parentId == -1) {
                                    return '<span class="layui-badge layui-bg-blue">目录</span>';
                                } else {
                                    return '<span class="layui-badge-rim">菜单</span>';
                                }
                            }, title: '类型'
                        },
                        {templet: '#auth-state', width: 250, align: 'center', title: '操作'}
                    ]
                ]
            });
        };

        renderTable();

        /** 工具条点击事件 **/
        table.on('tool(auth-table)', function (obj) {
            const data = obj.data;
            const layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'add') { // 添加下级
                if (obj.data.isMenu === 1) {
                    layer.msg("按钮无法添加下级", {icon: 2});
                    return false;
                }
                const nDate = {parentId: obj.data.id};
                showEditModel(nDate);
            } else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此权限吗？', function () {
                    layer.load(2);
                    $.post(basePath + 'system/menu/delete', {
                        meunId: obj.data.id
                    }, function (data) {
                        layer.closeAll('loading');
                        if (data.success) {
                            layer.msg(data.msg, {icon: 1});
                            renderTable();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    });
                });
            }
        });

        /** 添加菜单 **/
        $('#auth-btn-add').click(function () {
            showEditModel();
        });

        // 显示表单弹窗
        const showEditModel = function (data) {
            const title = (data && data.id) ? '修改菜单' : '添加菜单';
            admin.putTempData('t_menu', data);
            admin.popupCenter({
                title: title,
                path: basePath + 'system/menu/editForm',
                finish: function () {
                    renderTable();
                }
            });
        };

        // 搜索按钮点击事件
        $('#auth-btn-search').click(function () {
            let keyword = $('#auth-edit-search').val();
            let searchCount = 0;
            $('#auth-table').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
                $(this).css('background-color', 'transparent');
                const text = $(this).text();
                if (keyword != '' && text.indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        $('.layui-tab-item.layui-show').stop(true);
                        $('.layui-tab-item.layui-show').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (keyword != '' && searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            } else {
                treeTable.expandAll('#auth-table');
            }
        });
    });
</script>